package com.one.controller;

import com.one.service.SubscribeProductService;
import com.one.util.RequestReturn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName: SubscribeProductController
 * @Auther: yph
 * @Date: 2022/1/19 09:39
 * @Description: 订阅商品操作入口
 */
@Slf4j
@Api(tags = "N订阅商品操作入口")
@RestController
public class SubscribeProductController {

    @Autowired
    private SubscribeProductService subscribeProductService;

    /**
     * @Author: yph
     * @Date: 2022/1/16 21:36
     * @return:
     * @Description: 订阅商品到货提醒
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", required = true, value = "需要订阅的商品id"),
            @ApiImplicitParam(name = "phone", required = true, value = "手机号")
    })
    @ApiOperation(value = "订阅商品到货提醒", notes = "用户输入手机号，可以订阅库存不足的商品到货提醒")
    @GetMapping("/subscribeAddProduct")
    public RequestReturn<String> subscribeAddProduct(@RequestParam("phone") String phone,
                                                     @RequestParam("productId") Integer productId) {
        return subscribeProductService.subscribeAddProduct(phone, productId);
    }

    /**
     * @Author: yph
     * @Date: 2022/1/3 13:58
     * @return:
     * @Description: 添加库存
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", required = true, value = "根据商品id 添加库存"),
            @ApiImplicitParam(name = "addNum", value = "添加库存，默认是100")
    })
    @ApiOperation(value = "添加库存", notes = "根据商品id添加商品库存，给订阅过到货提醒的用户发送消息")
    @GetMapping("/addProduct")
    public RequestReturn<Map<String,Object>> addProduct(@RequestParam("productId") Integer productId, @RequestParam(value = "addNum", required = false) Integer addNum) {
        if (addNum == null) {
            addNum = 100;
        }
        return subscribeProductService.addProduct(productId, addNum);
    }

    /**
     * @Author: yph
     * @Date: 2022/1/18 15:12
     * @return:
     * @Description: 根据手机号查询收到的消息
     */
    @ApiImplicitParam(name = "phone", required = true, value = "订阅商品到货提醒的手机号")
    @ApiOperation(value = "接收短信", notes = "输入用户手机号，可以得到发送的消息")
    @GetMapping("/receiveMessage")
    public RequestReturn<String> receiveMessage(@RequestParam("phone") String phone) {
        return subscribeProductService.receiveMessage(phone);
    }

    /**
     * @Author: yph
     * @Date: 2022/1/22 01:10
     * @return:
     * @Description: 根据商品id 获取商品名称和库存状态
     */
    @ApiOperation(value = "查询商品基本信息", notes = "输入商品id，获取商品基本信息（库存、名字等）")
    @ApiImplicitParam(name = "productId", value = "商品Id")
    @GetMapping("/")
    public RequestReturn<Map<String, Object>> queryProductStock(@RequestParam("productId") Integer productId) {
        return subscribeProductService.queryProductStock(productId);
    }


}
